package com.apes.auto.car.service;

import com.apes.auto.car.model.CarBrand;
import com.apes.auto.car.model.Manufacturers;
import com.apes.auto.car.model.VehicleLabel;
import com.apes.auto.car.repository.VehicleLabelRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.area.model.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName VehicleLabel
 * @Description TODO
 * @Author pengyb
 * @Date 2019/7/12 11:52
 * @Version 1.0
 **/
@Service("vehicleLabelService")
public class VehicleLabelService extends DomainService {
    @Autowired
    private VehicleLabelRepository vehicleLabelRepository;

    public VehicleLabel save(SimpleRequest request) {
        VehicleLabel o = request.getO(VehicleLabel.class);
        return vehicleLabelRepository.saveAndFlush(o);

    }

    public String delete(SimpleRequest request) {
        VehicleLabel o = request.getO(VehicleLabel.class);
        vehicleLabelRepository.delete(o.getId());
        return "删除成功";

    }

    /**
     * 新增检查
     * @param GB
     * @param carBrand
     * @param manufacturers
     * @param YJFS
     * @param flag
     * @return
     */
    public VehicleLabel matchedVehicleLabel(Country GB, CarBrand carBrand, Manufacturers manufacturers, String YJFS, boolean flag) {
        //新增 厂牌
        VehicleLabel v = new VehicleLabel();

        // “厂牌编码”=国别编码+品牌首字母+厂商首字母
        try {
            v.setId(v.checkID(GB.getId().substring(0, 1), carBrand.getName(), manufacturers.getName()));

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (flag){
         v.setName(manufacturers.getName());
        }else {
            if (YJFS.equals("进口")) {
                v.setName(carBrand.getName() + "_" + manufacturers.getName() + "(进口)");
            } else {
                v.setName(carBrand.getName() + "_" + manufacturers.getName());

            }
        }
        v.setCarBrand(carBrand);
        v.setManufacturers(manufacturers);
        return vehicleLabelRepository.saveAndFlush(v);
    }

}
